﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using EnvSafe.Expression;

namespace Heroius.DBTransitor
{
    public partial class FormExpressionBuilder : Form
    {
        internal FormExpressionBuilder(string[] Fields, string OldExpression = "")
        {
            InitializeComponent();
            ListSource.Items.AddRange(Fields);
            ListOps.Items.AddRange(Step3Helper.Fac.GetAvailableOperatorNames());
            TbExp.Text = OldExpression;
        }

        /// <summary>
        /// 获取最后一次合法通过的计算过程
        /// </summary>
        internal Procedure LatestProc { get; private set; }

        internal string LatestExpression { get; private set; }

        private void ListOps_DoubleClick(object sender, EventArgs e)
        {
            if (ListOps.SelectedIndex != -1)
            {
                string Op = ListOps.SelectedItem.ToString();
                int p = TbExp.SelectionStart;
                TbExp.SelectedText = Op + "()";
                TbExp.Focus();
                TbExp.SelectionStart = p + Op.Length + 1;
            }
        }

        private void BtnClear_Click(object sender, EventArgs e)
        {
            TbExp.Clear();
            TbExp.Focus();
            TbExp.SelectionStart = 0;
        }

        private void BtnAccept_Click(object sender, EventArgs e)
        {
            try
            {
                LatestProc = Step3Helper.Maker.GenerateProcedure(TbExp.Text);
                LatestExpression = TbExp.Text;
                EventHandler h = this.OnProcedureGenerated;
                if (h != null) h(this, new EventArgs());
            }
            catch (ExpressionException ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void ListSource_DoubleClick(object sender, EventArgs e)
        {
            if (ListSource.SelectedIndex != -1)
            {
                string Fn = ListSource.SelectedItem.ToString();
                int p = TbExp.SelectionStart;
                TbExp.SelectedText = Fn;
                TbExp.Focus();
                TbExp.SelectionStart = p + Fn.Length;
            }
        }
        
        private void keyboard_Click(object sender, EventArgs e)
        {
            Button CurrentBtn = sender as Button;
            Input(CurrentBtn.Text);
        }

        void Input(string Content)
        {
            int p = TbExp.SelectionStart;
            TbExp.SelectedText = Content;
            TbExp.Focus();
            TbExp.SelectionStart = p + Content.Length; 
        }

        private void BtnDone_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void button4_Click(object sender, EventArgs e)
        {
            Input("!=");
        }

        private void button8_Click(object sender, EventArgs e)
        {
            Input(">=");
        }

        private void button7_Click(object sender, EventArgs e)
        {
            Input("<=");
        }

        internal event EventHandler OnProcedureGenerated;
    }
}
